package com.github.tifezh.kchart;

import com.github.tifezh.kchart.chart.MinuteLineEntity;
import com.github.tifezh.kchart.mock.BaseInfo;
import com.github.tifezh.kchart.mock.Data;
import com.github.tifezh.kchart.mock.TimeLineItemListBean;
import com.github.tifezh.kchart.mock.TradePeriodsBean;
import com.github.tifezh.kchart.mock.ValueBean;
import com.github.tifezh.kchartlib.chart.model.TimeInfo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        BaseInfo baseInfo = Data.baseInfo();

        ValueBean bean = baseInfo.value.get(0);
        List<MinuteLineEntity> data = new ArrayList<>();

        List<TimeInfo> timeInfos = new ArrayList<>();
        int count = 0;
        for (TradePeriodsBean item : bean.tradePeriods) {
            timeInfos.add(new TimeInfo(item.startTime, item.endTime));
            count += item.endTime - item.startTime;
        }
        System.out.println(count / 1000 / 60);

        long startTime = 0;
        long endTime = 0;
        int timeIndex = -1;
        int timeS = 1000 * 60;


        for (TimeLineItemListBean info : bean.timeLineItemList) {
            if (info.time > endTime) {
                System.out.println("休息时间");
                timeIndex++;
                startTime = bean.tradePeriods.get(timeIndex).startTime;
                endTime = bean.tradePeriods.get(timeIndex).endTime;
            }
            if (info.time < startTime) {
                System.out.println("跳过");
                continue;
            }

            while (info.time > startTime) {
                System.out.println("补数据" + startTime / 1000 / 60);
                //数据复制
                startTime += timeS;
                data.add(getEntity(info, startTime));
            }
            data.add(toEntity(info));
            System.out.println(startTime / 1000 / 60);
            startTime += timeS;
        }
        System.out.println("总共" + count / 1000 / 60);
        System.out.println("已加载" + data.size());
        System.out.println("未加载" + (count / 1000 / 60 - data.size()));
        System.out.println("倒数未加载" + (endTime - bean.timeLineItemList.get(bean.timeLineItemList.size() - 1).time) / 1000 / 60);

        int time = getPosition(timeInfos, 1555407600000l + 900 * 60000);
        System.out.println("index:" + time);

    }


    //根据时间获取下标
    private static int getPosition(List<TimeInfo> timeInfos, long time) {
        int index = 0;
        boolean isFag = false;
        for (TimeInfo info : timeInfos) {
            if (time >= info.startTime && time <= info.endTime) {
                //当前区域
                index += time - info.startTime;
                isFag = true;
                break;
            } else {
                index += info.endTime - info.startTime;
            }
        }
        if (isFag) {
            return index / 60000;
        } else {
            return -1;
        }
    }

    public static MinuteLineEntity getEntity(TimeLineItemListBean bean, long startTime) {
        MinuteLineEntity entity = new MinuteLineEntity();
        entity.price = bean.current.floatValue();
        entity.time = new Date(startTime);
        return entity;
    }

    public static MinuteLineEntity toEntity(TimeLineItemListBean bean) {
        MinuteLineEntity entity = new MinuteLineEntity();
        entity.price = bean.current.floatValue();
        entity.time = new Date(bean.time);
        return entity;
    }

    public void test1() {
        ChartListInfo chartListInfo = new ChartListInfo();

        List<MinuteLineEntity> data = new ArrayList<>();

        for (ChartGroupInfo item : chartListInfo.list) {
            long startTime = item.getOpenTime();
            long endTime = item.getEndTime();
            for (ChartInfo info : item.timeLineItemList) {
                if (info.getTime() < startTime) {
                    continue;
                }
                while (info.getTime() > startTime) {
                    //数据复制
                    startTime++;
                    data.add(info.getEntity(startTime));
                }
                data.add(info.toEntity());
                startTime++;
            }
        }
        System.out.println(data);
    }


    public void test2() {
        List<Time> times = null;
        List<ChartInfo> timeLineItemList = null;

        long startTime = 0;
        long endTime = 0;
        int timeIndex = -1;

        List<MinuteLineEntity> data = new ArrayList<>();

        for (ChartInfo info : timeLineItemList) {
            if (info.getTime() > endTime) {
                timeIndex++;
                startTime = times.get(timeIndex).start;
                endTime = times.get(timeIndex).end;
            }

            while (info.getTime() > startTime) {
                //数据复制
                startTime++;
                data.add(info.getEntity(startTime));
            }
            data.add(info.toEntity());
            startTime++;
        }
    }

    static class Time {
        long start;
        long end;
    }


    static class ChartListInfo {
        long openTime;
        long endTime;
        long tradeDate;
        List<ChartGroupInfo> list;


        public long getOpenTime() {
            return openTime / 1000 / 60;
        }

        public long getEndTime() {
            return endTime / 1000 / 60;
        }
    }

    static class ChartGroupInfo {
        long openTime;
        long endTime;
        BigDecimal lastclose;
        List<ChartInfo> timeLineItemList;

        public long getOpenTime() {
            return openTime / 1000 / 60;
        }

        public long getEndTime() {
            return endTime / 1000 / 60;
        }
    }

    static class ChartInfo {
        long time;
        BigDecimal current;

        /**
         * 分钟
         *
         * @return
         */
        public long getTime() {
            return time / 1000 / 60;
        }

        public MinuteLineEntity getEntity(long time) {
            MinuteLineEntity entity = new MinuteLineEntity();
            entity.time = new Date(time);
            entity.price = current.floatValue();
            return entity;
        }

        public MinuteLineEntity toEntity() {
            MinuteLineEntity entity = new MinuteLineEntity();
            entity.time = new Date(time);
            entity.price = current.floatValue();
            return entity;
        }
    }


}
